<template>
  <el-container>
    <el-main class="main-bg">
      <el-form ref="searchForm" :model="searchForm" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="类型名称">
              <el-input
                v-model="searchForm.sortName"
                placeholder="请输入类型名称"
              />
            </el-form-item>
          </el-col>
          <el-col :offset="12" :span="6" class="text-right">
            <el-button
              icon="iconfont iconxianxing_sousuo"
              type="primary"
              @click="handleSearch"
            >
              查询
            </el-button>
            <el-button
              icon="iconfont iconxianxing_zhongzhi"
              @click="handleReset"
            >
              重置
            </el-button>
          </el-col>
          <el-col :span="24" class="text-left">
            <el-button type="primary" @click="onAdd">
              新增字典
            </el-button>
          </el-col>
        </el-row>
      </el-form>
      <Table
        ref="table"
        :current-page="page"
        :header-data="tableHeader"
        :index="false"
        :loading="loading"
        :page-size="limit"
        :selection="true"
        :table-data="tableData"
        :total="total"
        class="data-table"
        style="width: 100%"
        @select-change="onSelectionChange"
        @now-page="handlePage"
        @now-size="handleSize"
        @table-sort="handleSort"
      />
    </el-main>
    <el-drawer :title="title" :visible.sync="drawerVisible">
      <div class="drawer-main">
        <el-form ref="form" :model="form" label-position="top" label-width="80px">
          <el-form-item label="字典名称" required>
            <el-input v-model="form.sortName" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="字典类型" required>
            <el-select v-model="form.sortType" placeholder="请选择">
              <el-option v-for="item in sortTypeList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" placeholder="请输入" type="textarea" />
          </el-form-item>
        </el-form>
      </div>
      <div v-if="type!=='look'" class="drawer-footer">
        <div class="footer-btn">
          <el-button type="primary" @click="handleSubmit"> 确认</el-button>
          <el-button @click="drawerVisible = false"> 取消</el-button>
        </div>
      </div>
    </el-drawer>
  </el-container>
</template>

<script>
import addTable from '@/mixins/addtable'
import { getSortList, postSortDel, postSortAdd, postSortUpdate } from './api'

export default {
  name: 'SortList',
  mixins: [addTable],
  data() {
    return {
      searchForm: {
        sortName: ''
      },
      title: '',
      type: 'look',
      tableHeader: [],
      personOptions: [],
      oneLoading: false,
      drawerVisible: false,
      sortTypeList: [
        { label: '类型', value: '1' },
        { label: '计数单位', value: '2' },
      ],
      form: {
        sortId: '',
        sortName: '',
        sortType: '',
        remark: ''
      }
    }
  },
  created() {
    const _this = this
    this.tableHeader = [
      {
        prop: 'sortId',
        label: '字典编码',
        sortable: false,
        overflow: true,
        width: '180px',
        align: 'left'
      },
      {
        prop: 'sortName',
        label: '字典名称',
        sortable: false,
        overflow: true,
        width: 180,
        align: 'left'
      },
      {
        prop: 'sortType',
        label: '字典类型',
        sortable: false,
        overflow: true,
        align: 'left',
        render: (h, vm) => {
          const arr = _this.sortTypeList.filter((item) => {
            return vm.sortType === item.value
          })
          return <span>{arr[0].label}</span>
        }
      },
      {
        prop: 'remark',
        label: '备注',
        sortable: false,
        overflow: true,
        align: 'left'
      },
      {
        width: 150,
        label: '操作',
        align: 'left',
        fixed: 'right',
        buttonList: (row, index) => {
          return [
            {
              text: '详情',
              type: 'text',
              event: this.onDetail
            },
            {
              text: '编辑',
              type: 'text',
              event: this.onEdit
            },
            {
              text: '删除',
              type: 'text',
              event: this.onDelete
            }
          ]
        }
      }
    ]
    this.getTableData()
  },

  methods: {
    onDetail(row) {
      this.form = {
        ...row
      }
      this.type = 'look'
      this.title = '查看详情'
      this.drawerVisible = true
    },
    onEdit(row) {
      this.form = {
        ...row
      }
      this.type = 'edit'
      this.title = '编辑类型'
      this.drawerVisible = true
    },
    onAdd() {
      this.form = {}
      this.type = 'add'
      this.title = '新增类型'
      this.drawerVisible = true
    },
    onDelete(row) {
      this.$confirm('您确认要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        postSortDel({ sortId: row.sortId })
            .then(({ success, message }) => {
              if (success) {
                this.$message.success(message)
              } else {
                this.$message.error(message)
              }
              this.getTableData()
            })
            .catch()
      })
    },
    getTableData() {
      this.loading = true
      this.getTableParams()
      getSortList(this.tableParams)
          .then((res) => {
            if (!res) return
            const { data, pageInfo } = res
            setTimeout(() => {
              this.loading = false
              this.tableData = data
              this.total = pageInfo.total
            }, 500)
          })
          .catch((err) => {
            console.log('TCL: getTableData -> err', err)
          })
    },
    handleSubmit() {
      this.form.sortId
          ? postSortUpdate(this.form).then(({ success, message }) => {
            if (success) {
              this.$message.success(message)
            } else {
              this.$message.error(message)
            }
            this.drawerVisible = false
            this.form = {}
            this.getTableData()
          })
          : postSortAdd(this.form).then(({ success, message }) => {
            if (success) {
              this.$message.success(message)
            } else {
              this.$message.error(message)
            }
            this.drawerVisible = false
            this.form = {}
            this.getTableData()
          })
    },
    onSelectionChange() {

    }
  }
}
</script>
